class Solution {
public:
    bool isHappy(int n) {
        int slow = cal(n);
        int fast = cal(cal(n));
        while (fast > 1)
        {
            if (slow == fast)
                return false;
            slow = cal(slow);
            fast = cal(cal(fast));
        }

        return true;
    }
    int cal(int n)
    {
        int result = 0;
        while (n > 0)
        {
            int temp = n % 10;
            result += temp * temp;
            n = n / 10;
        }
        return result;
    }
};